import re
import requests


def get_data(url, f):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
    }
    response = requests.get(url, headers=headers)

    html = response.content.decode('utf-8')

    ret = re.findall('<div class="content">.+?<span>(.+?)</span>', html, re.DOTALL)

    for i in ret:
        all_data = re.sub('<.*?>', '', i)
        f.write('{}\n\n\n\n'.format(all_data.strip(),50))


def save():
    base_url = "https://www.qiushibaike.com/text/page/{}/"
    for x in range(1, 11):
        with open('duanzi.txt', 'a', encoding='utf-8') as f:
            get_data(base_url.format(x), f)


if __name__ == '__main__':
    save()
